package com.sensePlusVR.activity.user;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sensePlusVR.activity.listener.AEditTextCallBack;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.main.R;
import com.sensePlusVR.net.NetRequestManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.EditTextUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.MyToast;
import com.sensePlusVR.utils.StringUtils;
import com.sensePlusVR.widget.EditTextDeleteAll;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_forgot_password)
public class ForgotPwdActivity extends BaseActivity{
	private static final String TAG = ForgotPwdActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;
	@ViewInject(R.id.forgot_email_edit)
	private EditTextDeleteAll forgot_email_edit;
	
	@ViewInject(R.id.forgot_delete_iv)
	private ImageView forgot_delete_iv;
	
	private String loginName;
	
	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}
	
	@Override
	public void beforeView() {
		super.beforeView();
		AppManager.getInstance().addActivity(this);
		loginName = MySharedPreferences.getString2(ForgotPwdActivity.this,MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_LOGINNAME);
		title_bar_title_tv.setText(getResources().getString(R.string.forgot_password));
		forgot_email_edit.setText(loginName);
		forgot_email_edit.setSelection(loginName.length());
		
		forgot_email_edit.setOnAEditTextCallBack(editTextCallBack);
	}
	
	@Event(value = {R.id.title_bar_left_ib,R.id.forgot_submit,R.id.forgot_delete_iv})
	private void setViewOnClick(View v){
		switch (v.getId()) {
		case R.id.title_bar_left_ib:
			finishActivity();
			break;
		case R.id.forgot_submit:
			MyLog.d(TAG, "forgot_submit...");
			forgotPassword();
			break;
		case R.id.forgot_delete_iv:
			forgot_email_edit.setText("");
			break;
		}
	}

	private void forgotPassword() {
		String email = EditTextUtils.getText(forgot_email_edit);
	    if (StringUtils.isEmpty(email)) {
            MyToast.showMessage(getApplicationContext(),
                    R.string.msg_common_e_null_email);
            return;
        }
	    if (!StringUtils.isEmail(email)) {
        	MyToast.showMessage(getApplicationContext(),
                    R.string.msg_common_e_phone_number_email);
            return;
        }
		NetRequestManager.startForgotPassword(ForgotPwdActivity.this, /*loginName,*/ email.trim());
		MyPageJump.pageJump(ForgotPwdActivity.this, LoginActivity.class);
		//finishActivity();
	}
	
	private AEditTextCallBack editTextCallBack = new AEditTextCallBack() {
		
		@Override
		public void onDeleteVisible(View view, boolean isVisible) {
			switch (view.getId()) {
			case R.id.forgot_email_edit:
				if(isVisible){
					
					forgot_delete_iv.setVisibility(View.VISIBLE);
					
				}else{
					
					forgot_delete_iv.setVisibility(View.INVISIBLE);
					
				}
				
				break;
			}
		}
	};
	
	private void finishActivity(){
		AppManager.getInstance().finishActivity(this);
	}
	
	@Override
	public void onBackPressed() {
		finishActivity();
	}
}
